package com.hrabun.goodmorningalarm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;

public class Database {
	
	Context ctx;
	SimpleDateFormat sdf;
	DateFormat df;
	ArrayList<AlarmItem> alarmsList;
	
	public Database(Context _ctx) {
		ctx = _ctx;
	}
	
	public ArrayList<AlarmItem> getAllAlarms() {
		
		AlarmItem aItem = null;
		alarmsList = new ArrayList<AlarmItem>();

		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		Cursor c = db.query(Global.DB_TABLE_NAME, null, null, null, null, null, null);
		if (c.moveToFirst()) {
			do {
			aItem = new AlarmItem(c.getString(Global.DB_ALARM_NAME_INDEX), 
						Global.convertFromStringToDate((c.getString(Global.DB_ALARM_TIME_INDEX))), 
						Global.getStateFromString(c.getString(Global.DB_MONDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_TUESDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_WEDNESDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_THURSDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_FRIDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_SATURDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_SUNDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_ACTIVE_INDEX)));
			aItem.setId(c.getString(Global.DB_ID_INDEX));
			aItem.setRadioAssigned(c.getString(Global.DB_ALARM_ITEM_RADIO_ID));
			alarmsList.add(aItem);
			}  while (c.moveToNext());
		}
		c.close();
		db.close();
		return alarmsList;
		
	}
	
	public ArrayList<AlarmItem> getAllActiveAlarms () {
		AlarmItem aItem = null;
		alarmsList = new ArrayList<AlarmItem>();

		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		Cursor c = db.query(Global.DB_TABLE_NAME, null, Global.DB_COLUMN_ACTIVE +" = 1", null, null, null, null);
		if (c.moveToFirst()) {
			do {
			aItem = new AlarmItem(c.getString(Global.DB_ALARM_NAME_INDEX), 
						Global.convertFromStringToDate((c.getString(Global.DB_ALARM_TIME_INDEX))), 
						Global.getStateFromString(c.getString(Global.DB_MONDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_TUESDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_WEDNESDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_THURSDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_FRIDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_SATURDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_SUNDAY_INDEX)), 
						Global.getStateFromString(c.getString(Global.DB_ACTIVE_INDEX)));
			aItem.setId(c.getString(Global.DB_ID_INDEX));
			aItem.setRadioAssigned(c.getString(Global.DB_ALARM_ITEM_RADIO_ID));
			alarmsList.add(aItem);
			}  while (c.moveToNext());
		}
		c.close();
		db.close();
		return alarmsList;
	}
	
	public AlarmItem getAlarmItemByTime(Date aTime) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		Cursor c = db.query(Global.DB_TABLE_NAME, null, Global.DB_COLUMN_ALARM_TIME+"="+aTime.toString(), null, null, null, null);
		AlarmItem aItem = null;
		if (c.moveToFirst()) {
			aItem = new AlarmItem(c.getString(Global.DB_ALARM_NAME_INDEX), Global.convertFromStringToDate(c.getString(Global.DB_ALARM_TIME_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_MONDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_TUESDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_WEDNESDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_THURSDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_FRIDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_SATURDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_SUNDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_ACTIVE_INDEX)));
			aItem.setId(c.getString(Global.DB_ID_INDEX));
			aItem.setRadioAssigned(c.getString(Global.DB_ALARM_ITEM_RADIO_ID));
		} while (c.moveToNext());
		c.close();
		db.close();
		return aItem;
	}
	
	public AlarmItem getAlarmItemById(String id) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		Cursor c = db.query(Global.DB_TABLE_NAME, null, Global.DB_COLUMN_ALARM_ID+"="+id, null, null, null, null);
		AlarmItem aItem = null;
		if (c.moveToFirst()) {
			aItem = new AlarmItem(c.getString(Global.DB_ALARM_NAME_INDEX), Global.convertFromStringToDate(c.getString(Global.DB_ALARM_TIME_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_MONDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_TUESDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_WEDNESDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_THURSDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_FRIDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_SATURDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_SUNDAY_INDEX)), 
					Global.getStateFromString(c.getString(Global.DB_ACTIVE_INDEX)));
			aItem.setId(c.getString(Global.DB_ID_INDEX));
			aItem.setRadioAssigned(c.getString(Global.DB_ALARM_ITEM_RADIO_ID));
		} while (c.moveToNext());
		c.close();
		db.close();
		return aItem;
	}

	public int deleteAllAlarms() {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		int rowsAffected = db.delete(Global.DB_TABLE_NAME, null, null);
		db.close();
		return rowsAffected;
	}
	
	public String saveAlarmItem(AlarmItem aItem) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
    	cv.put(Global.DB_COLUMN_ALARM_NAME, aItem.alarmName);
    	cv.put(Global.DB_COLUMN_ALARM_TIME, Global.convertFromDateToString(aItem.alarmTime));
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_MONDAY, aItem.monday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_TUESDAY, aItem.tuesday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_WEDNESDAY, aItem.wednesday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_THURSDAY, aItem.thursday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_FRIDAY, aItem.friday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_SATURDAY, aItem.saturday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_SUNDAY, aItem.sunday);
    	cv.put(Global.DB_COLUMN_ACTIVE, aItem.active);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_RADIO, aItem.radioAssignedID);
    	long itemId = db.insert(Global.DB_TABLE_NAME, null, cv);
    	
    	dbh.close();
    	
    	return String.valueOf(itemId);
	}
	
	public void changeActiveState(String id, Boolean newState) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(Global.DB_COLUMN_ACTIVE, newState);
		db.update(Global.DB_TABLE_NAME, cv, Global.DB_COLUMN_ALARM_ID+"="+id, null);
		db.close();		
	}

	public void deleteAlarmItem(String id) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		db.delete(Global.DB_TABLE_NAME, Global.DB_COLUMN_ALARM_ID+"="+id, null);
		db.close();
	}

	public int updateAlarmItem(AlarmItem aItem) {
		DBHelper dbh = new DBHelper(ctx);
		SQLiteDatabase db = dbh.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
    	cv.put(Global.DB_COLUMN_ALARM_NAME, aItem.alarmName);
    	cv.put(Global.DB_COLUMN_ALARM_TIME, Global.convertFromDateToString(aItem.alarmTime));
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_MONDAY, aItem.monday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_TUESDAY, aItem.tuesday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_WEDNESDAY, aItem.wednesday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_THURSDAY, aItem.thursday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_FRIDAY, aItem.friday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_SATURDAY, aItem.saturday);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_SUNDAY, aItem.sunday);
    	cv.put(Global.DB_COLUMN_ACTIVE, aItem.active);
    	cv.put(Global.DB_COLUMN_ALARM_ITEM_RADIO, aItem.radioAssignedID);
    	
    	int result = db.update(Global.DB_TABLE_NAME, cv, Global.DB_COLUMN_ALARM_ID+" = ?", new String[] {aItem.id});
    	db.close();
    	return result;
	}
	
		
}
